/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:scpii-mobi-page</p>
 *@author xiezhouyan
 *@Date 2013-4-28 上午11:22:23
 *@version 1.0
 *@description:
 */
package com.scpii.mobi.page.util;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class NetUtils {

	public static final ObjectMapper MAPPER = new ObjectMapper();

	public static String sendPost(String url, String contentType,
			Map<String, String> params) throws JsonGenerationException,
			JsonMappingException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		if (StringUtils.contains(contentType, "application/json")) {
			httpPost.setHeader("Content-Type", "application/json");
		}
		StringEntity data = new StringEntity(MAPPER.writeValueAsString(params),
				Charset.forName("UTF-8"));
		httpPost.setEntity(data);
		HttpResponse response = client.execute(httpPost);
		return EntityUtils.toString(response.getEntity());

	}
}
